package com.fzx.fzwsupplysystem.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import com.fzx.fzwsupplysystem.R;
import com.fzx.fzwsupplysystem.bean.PersonBean;
import com.fzx.fzwsupplysystem.util.AppConstant;
import com.fzx.fzwsupplysystem.util.CountTimes;
import com.fzx.fzwsupplysystem.view.CountTimeTextView;

import java.io.Serializable;

public class CardCheckActivity extends BaseActivity implements View.OnClickListener {

    private CountTimeTextView tv_count;
    private Button btn_back;
    private Button btn_submit;
    private Button btn_cancel;

    private PersonBean personBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_check);
        initView();
        Intent intent = getIntent();
        personBean = (PersonBean) intent.getSerializableExtra("PersonBean");
    }

    private void initView() {
        tv_count = findViewById(R.id.tv_count);
        btn_back = findViewById(R.id.btn_back);
        btn_submit = findViewById(R.id.btn_submit);
        btn_cancel = findViewById(R.id.btn_cancel);
        btn_back.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
        btn_cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back:
                gotoHome();
                break;
            case R.id.btn_submit:
                //进入个人信息页面
                Intent intent = new Intent(this, PersonalInfoActivity.class);
                intent.putExtra("PersonBean", (Serializable) personBean);
                startActivity(intent);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                break;
            case R.id.btn_cancel:
                gotoHome();
                break;
        }

    }

    private void startTimeDown() {
        tv_count.startTimeDown(AppConstant.CountTime, 1, new CountTimes.OnCountTimeListener() {
            @Override
            public void onStart(int time) {
                tv_count.setText("" + time);
            }

            @Override
            public void onTick(int time) {
                tv_count.setText("" + time);
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFinish(int time) {
                gotoHome();
            }
        });
    }

    private void releaseTimeDown() {
        tv_count.release();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        tv_count.resetTime();
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onResume() {
        super.onResume();
        startTimeDown();
    }

    @Override
    protected void onStop() {
        super.onStop();
        releaseTimeDown();
    }
}
